package org.simplesocket.simplenettycore.protocol;

import io.netty.util.Recycler;
import lombok.Getter;
import lombok.Setter;
import org.simplesocket.simplenettycore.session.ClientSessionContext;

import java.util.Map;

public class Request {

    @Setter
    @Getter
    private String uri = "";
    
    @Setter
    @Getter
    private Map<String,?> params = null;
    
    @Setter
    @Getter
    private ClientSessionContext csc;
    
    @Setter
    @Getter
    private String sessionID;
    
    private final Recycler.Handle<Request> handle;
    
    public Request(Recycler.Handle<Request> handle) {
        this.handle = handle;
    }
    
    public static final Recycler<Request> RECYCLER = new Recycler<>() {
        @Override
        protected Request newObject(Handle<Request> handle) {
            return new Request(handle);
        }
    };
    
    public static Request newInstance(){
        return RECYCLER.get();
    }
    
    public void recycle() {
        // 重置字段
        reset();
        handle.recycle(this);
    }
    
    private void reset(){
        this.setUri("");
        this.setParams(null);
    }
    
    public static class Builder{
        
        private final Request request;
        
        public Builder() {
            this.request = Request.newInstance();
        }
        
        public Builder uri(String uri){
            this.request.setUri(uri);
            return this;
        }
        
        public Builder params(Map<String,?> params){
            this.request.setParams(params);
            return this;
        }
        
        public Builder csc(ClientSessionContext csc){
            this.request.setCsc(csc);
            return this;
        }
        
        public Builder sessionID(String id){
            this.request.setSessionID(id);
            return this;
        }
        
        public Request build(){
            return request;
        }
    }
    
}
